﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace AliCMS.TemplateEngine
{
    internal class Widget : Tag
    {
        internal Widget(Engine engine, int index, int length, int contentIndex, int contentLength, Tag parent)
            : base(engine, index, length, contentIndex, contentLength, parent)
        {
        }

        internal override void Parse()
        {
            ParseToCode();
            ParseToXml();
        }

        private void ParseToCode()
        {
            StringBuilder result = new StringBuilder();

            StringBuilder replacement = new StringBuilder();
            foreach (Tag tag in ChildTags)
            {
                if (tag is Code)
                {
                    replacement.Append(tag.ResultCode);
                }
                else if (tag is OutputStatment)
                {
                    replacement.Append("object " + tag.UniqueName + " = " + tag.ResultCode + ";\r\n");
                    replacement.Append("result.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + " == null ? \"\" : " + tag.UniqueName + ".ToString());\r\n");
                }
                else if (tag is IfStatment)
                {
                    replacement.Append(tag.ResultCode);
                    replacement.Append("result.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + ");\r\n");
                }
                else if (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+"))
                {
                    replacement.Append("result.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + ");\r\n");
                }
            }

            result.Append(@"
                            protected string <%Name%>
                            {
                                get
                                {
                                    StringBuilder result = new StringBuilder(_xmlTemplate.SelectSingleNode(""/"" + ""/<%Name%>"").InnerText);
                                    <%Replacement%>
                                    return result.ToString();
                                }
                            }
                            ");
            result.Replace("<%Name%>", UniqueName);
            result.Replace("<%Replacement%>", replacement.ToString());

            ResultCode = result.ToString();
        }
        private void ParseToXml()
        {
            StringBuilder result = new StringBuilder(Engine.Text.Substring(ContentIndex, ContentLength));
            for (int i = ChildTags.Count - 1; i >= 0; i--)
            {
                Tag tag = ChildTags[i];
                result.Remove(tag.Index - ContentIndex, tag.Length);
                result.Insert(tag.Index - ContentIndex, "{#" + tag.UniqueName + "#}");
            }

            result.Insert(0, "<![CDATA[<Widget>");
            result.Append("</Widget>]]>");

            ResultXml = result.ToString();
        }
    }
}
